﻿<?php
/*
*	V.I.Plus registration form
*	Last update: 16/06/2013
*	Get support via support@viplus.com
*	Creator: Ilya Krashchyn
*/

// Server side input validations can be performed here
function get_param(&$value, $key){ 
	$value = strlen($value) <= 30 ? $value : ''; 
}

// Register contact in V.I.Plus service using specific API key
function register_viplus($api_key, $param) {
	$port = 80; $timeout = 15;
	$data = is_array($param) ? http_build_query($param) : $param;
	$url = parse_url('http://api.viplus.com/Gates/Ws-'.$api_key.'.asmx/RMembers_Import');
	if ($url['scheme'] != 'http') { die('Error: Only HTTP requests are supported !'); }
	$host = $url['host'];
	$path = $url['path'];
	try{
		$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
		if ($fp){
			fputs($fp, "POST $path HTTP/1.1\r\n");
			fputs($fp, "Host: $host\r\n");
			fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
			fputs($fp, "Content-length: ". strlen($data) ."\r\n");
			fputs($fp, "Connection: close\r\n\r\n");
			fputs($fp, $data);
			$result = '';
			while(!feof($fp))
				$result .= fgets($fp, 1024);
		}
		else return array(
			'status' => 'error',
			'error' => "$errstr ($errno)"
		);
		fclose($fp);
		
		// For quick debug you can uncomment these lines:
		//print_r($url);
		//echo '<br>';
		//print_r($data);
		//echo '<br>';
		//print_r($result);
		//echo '<br>';
		//die();
		
		$result = explode("\r\n\r\n", $result, 2);
		//$header = isset($result[0]) ? $result[0] : '';
		$content = isset($result[1]) ? $result[1] : '';
		$rVal = simplexml_load_string($content);
		$rText = $rVal[0];

		switch($rText){
			case 'OkInserted': 
				return array(
					'status' => 'ok',
					'error' => 'Success'
				);
				break;
			case 'ErrNotValid':
				return array(
					'status' => 'error',
					'error' => 'Data contains illegal chars'
				);
				break;
			case 'ErrExists':
				return array(
					'status' => 'error',
					'error' => 'Contact already exists'
				);
				break;
			case 'ErrBlackListed':
				return array(
					'status' => 'error',
					'error' => 'Contact in black list'
				);
				break;
			default:
				return array(
					'status' => 'error',
					'error' => 'Unknown error'
				);
				break;
		}
	}catch(Exception $e){
		return array(
			'status' => 'error',
			'error' => $e->getMessage()
		);
	}
}

// Process form data
if(extract($_POST) > 0 && (isset($email) or die('Email field is required'))){
	// Define API key here:
	$API_KEY = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX';
	// Define registration fields here:
	// Possible fields are: email, firstname, lastname, birthdate, phone, cellphone, address, city, country, company, position
	$fields = compact('email', 'firstname', 'lastname', 'phone');
	array_walk($fields, 'get_param');
	$rVal = register_viplus($API_KEY, $fields);
	if($rVal['status'] == 'ok')
		$message = 'Registered successfully';
	else
		$message = 'Error: '. $rVal['error'];
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
	 <head>
		<meta charset="utf-8">
		<title>V.I.Plus registration form for PHP example</title>
	 </head>

	 <body>
		<?php
			// Error message
			if(isset($message))
				echo '<h1>'. $message .'</h1>';
		?>
		<script type="text/javascript">
			// Validation example
			function validateForm(){
				return document.getElementById('email').value != '';
			}
		</script>
		<form method='POST' action='index.php' id='viplus_register_form' name='viplus_register_form' onsubmit="return validateForm();">
			<?php
				// Define registration fields here:
				// Possible fields are: email, firstname, lastname, birthdate, phone, cellphone, address, city, country, company, position
				$fields = Array(
					'email' => 'EMail', 
					'firstname' => 'First Name', 
					'lastname' => 'Last Name', 
					'phone' => 'Phone'
				);
				foreach($fields as $key=>$value){
					echo "<label for='$key'>$value: </label><input type='text' name='$key' id='$key'/><br/>";
				}
			?>
			<label for='submit' class='submit'>&nbsp;</label><input type='submit' name='submit' value='Register!' /><br/>
			<input type='hidden' name='RegisterViplus' value='true' /><br/>
		</form>
	 </body>
 </html>
